package net.synck.gcontacts.ui;

import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.forms.events.HyperlinkEvent;
import org.eclipse.ui.forms.events.IHyperlinkListener;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.forms.widgets.Form;
import org.eclipse.ui.forms.widgets.ExpandableComposite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.widgets.Hyperlink;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Table;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Button;

public class ContactEditorComposite extends Composite {

	private FormToolkit formToolkit = null;   //  @jve:decl-index=0:visual-constraint=""
	private Form form = null;
	private Text textName = null;
	private Label labelName = null;
	private Composite compositeMain = null;
	private Composite compositeEmail = null;
	private Composite compositeEmailEntries = null;
	private Hyperlink hyperlinkAddEmail = null;
	private Label labelEmail = null;
	private Text textEmail1 = null;
	private Combo comboEmailType1 = null;
	private Hyperlink hyperlinkRemoveEmail1 = null;
	public ContactEditorComposite(Composite parent, int style) {
		super(parent, style);
		initialize();
	}

	private void initialize() {
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 1;
		this.setLayout(gridLayout);
		createCompositeMain();
		setSize(new Point(655, 370));
		hookFormModificationActions();
	}

	/**
	 * This method initializes formToolkit	
	 * 	
	 * @return org.eclipse.ui.forms.widgets.FormToolkit	
	 */
	private FormToolkit getFormToolkit() {
		if (formToolkit == null) {
			formToolkit = new FormToolkit(Display.getCurrent());
		}
		return formToolkit;
	}

	private void hookFormModificationActions() {
		hyperlinkAddEmail.addHyperlinkListener( new IHyperlinkListener() {

			@Override
			public void linkActivated(HyperlinkEvent e) {
				// TODO Auto-generated method stub
				createEmailField();
				form.redraw();
				form.update();
			}

			@Override
			public void linkEntered(HyperlinkEvent e) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void linkExited(HyperlinkEvent e) {
				// TODO Auto-generated method stub
				
			}
			
		});
	}
	
	/**
	 * This method initializes form	
	 *
	 */
	private void createForm() {
		GridData gridData13 = new GridData();
		gridData13.horizontalAlignment = GridData.CENTER;
		gridData13.verticalAlignment = GridData.BEGINNING;
		GridData gridData7 = new GridData();
		gridData7.horizontalAlignment = GridData.FILL;
		gridData7.grabExcessHorizontalSpace = true;
		gridData7.grabExcessVerticalSpace = true;
		gridData7.verticalAlignment = GridData.FILL;
		GridLayout gridLayout1 = new GridLayout();
		gridLayout1.numColumns = 3;
		GridData gridData = new GridData();
		gridData.horizontalAlignment = GridData.FILL;
		gridData.grabExcessHorizontalSpace = true;
		gridData.verticalAlignment = GridData.CENTER;
		form = getFormToolkit().createForm(compositeMain);
		form.setText("Contact");
		form.setLayoutData(gridData7);
		form.getBody().setLayout(gridLayout1);
		labelName = getFormToolkit().createLabel(form.getBody(), "Name:");
		textName = getFormToolkit().createText(form.getBody(), null, SWT.SINGLE | SWT.BORDER);
		textName.setLayoutData(gridData);
		Label filler3 = new Label(form.getBody(), SWT.NONE);
		labelEmail = getFormToolkit().createLabel(form.getBody(), "Emails:");
		labelEmail.setLayoutData(gridData13);
		createCompositeEmail();
	}

	public void setContactName(String name) {
		this.textName.setText(name);
	}
	
	public String getContactName() {
		return this.textName.getText();
	}
	
	public void setModifyListener(ModifyListener listener) {
		if (listener != null) {
			textName.addModifyListener(listener);
		} else {
			//TODO remove listeners 
		}
	}

	/**
	 * This method initializes compositeMain	
	 *
	 */
	private void createCompositeMain() {
		GridData gridData4 = new GridData();
		gridData4.horizontalAlignment = GridData.FILL;
		gridData4.grabExcessHorizontalSpace = true;
		gridData4.grabExcessVerticalSpace = true;
		gridData4.verticalAlignment = GridData.FILL;
		compositeMain = getFormToolkit().createComposite(this);
		compositeMain.setLayout(new GridLayout());
		createForm();
		compositeMain.setLayoutData(gridData4);
	}

	/**
	 * This method initializes compositeEmail	
	 *
	 */
	private void createCompositeEmail() {
		GridData gridData3 = new GridData();
		gridData3.horizontalAlignment = GridData.END;
		gridData3.verticalAlignment = GridData.CENTER;
		GridData gridData1 = new GridData();
		gridData1.horizontalAlignment = GridData.FILL;
		gridData1.verticalAlignment = GridData.FILL;
		compositeEmail = getFormToolkit().createComposite(form.getBody());
		compositeEmail.setLayout(new GridLayout());
		createCompositeEmailEntries();
		compositeEmail.setLayoutData(gridData1);
		hyperlinkAddEmail = getFormToolkit().createHyperlink(compositeEmail, "Add...", SWT.NONE);
		hyperlinkAddEmail.setLayoutData(gridData3);
	}

	/**
	 * This method initializes compositeEmailEntries	
	 *
	 */
	private void createCompositeEmailEntries() {
		/*
		GridData gridData5 = new GridData();
		gridData5.grabExcessHorizontalSpace = true;
		gridData5.verticalAlignment = GridData.CENTER;
		gridData5.horizontalAlignment = GridData.FILL;
		GridLayout gridLayout2 = new GridLayout();
		gridLayout2.numColumns = 3;
		GridData gridData2 = new GridData();
		gridData2.horizontalAlignment = GridData.FILL;
		gridData2.grabExcessHorizontalSpace = true;
		gridData2.verticalAlignment = GridData.CENTER;
		compositeEmailEntries = getFormToolkit().createComposite(compositeEmail);
		compositeEmailEntries.setLayoutData(gridData2);
		compositeEmailEntries.setLayout(gridLayout2);
		textEmail1 = getFormToolkit().createText(compositeEmailEntries, null, SWT.SINGLE | SWT.BORDER);
		textEmail1.setLayoutData(gridData5);
		comboEmailType = new Combo(compositeEmailEntries, SWT.NONE);
		hyperlinkRemoveEmail1 = getFormToolkit().createHyperlink(compositeEmailEntries, "Remove", SWT.NONE);
		*/ 
		GridLayout gridLayout2 = new GridLayout();
		gridLayout2.numColumns = 3;
		GridData gridData2 = new GridData();
		gridData2.horizontalAlignment = GridData.FILL;
		gridData2.grabExcessHorizontalSpace = true;
		gridData2.verticalAlignment = GridData.FILL;
		compositeEmailEntries = getFormToolkit().createComposite(compositeEmail);
		compositeEmailEntries.setLayoutData(gridData2);
		compositeEmailEntries.setLayout(gridLayout2);
		
		createEmailField();		
	}
	
	private void createEmailField() {
		GridData gridData5 = new GridData();
		gridData5.grabExcessHorizontalSpace = true;
		gridData5.verticalAlignment = GridData.CENTER;
		gridData5.horizontalAlignment = GridData.FILL;
		Text textEmail = getFormToolkit().createText(compositeEmailEntries, null, SWT.SINGLE | SWT.BORDER);
		textEmail.setLayoutData(gridData5);
		Combo comboEmailType = new Combo(compositeEmailEntries, SWT.NONE);
		Hyperlink hyperlinkRemoveEmail = getFormToolkit().createHyperlink(compositeEmailEntries, "Remove", SWT.NONE); 
	}
	
	
	
}  //  @jve:decl-index=0:visual-constraint="10,10"
